import { ITimer } from "../../api/timer/ITimer";
import { E_TimeConfig } from "../defs/com/E_TimeConfig";
import { E_FunctionUnLockType } from "../defs/unlock/E_FunctionUnLockType";
import { CDFunctionUnlock } from "../excel/CDFunctionUnlock";
import { CDLordPraySkill } from "../excel/CDLordPraySkill";
import { Player } from "../struct/Player";
import { LanguageUtils } from "../../utils/LanguageUtils";
import { SDServer } from "./SDServer";
import { SDTrainTower } from "./SDTrialTower";


/**
 * 数据-功能开启
 */
export abstract class SDFuction {






    /**
     * 是否开启
     * @param player 
     * @param fuctionId 
     */
    public static isUnlock(player: Player, fuctionId: number): boolean {
        if (0 === fuctionId) {
            return true;
        }
        const config = CDFunctionUnlock.getData(fuctionId);
        if (!config) {
            return true;
        }

        //开服天数
        if (config.limitDay > 0) {
            const pass = ITimer.getServerTime() - SDServer.getServerCreateTime(player);
            if (pass < config.limitDay * E_TimeConfig.Day) {
                return false;
            }
        }

        const u1 = this.checkUnlock(player, config.type, config.value);
        if (0 === config.conditiontype) {
            return u1;
        }

        const u2 = this.checkUnlock(player, config.type_2, config.value_2);
        if (1 === config.conditiontype) {
            return u1 && u2;
        }
        else {
            return u1 || u2;
        }
    }


    /**
     * 检测类型
     * @param player 
     * @param k 
     * @param v 
     */
    protected static checkUnlock(player: Player, k: number, v: number[]): boolean {
        if (k === E_FunctionUnLockType.Lock) {
            return false;
        }

        const defaultv = v[0];

        switch (k) {
            case E_FunctionUnLockType.PlayerLevel: return player.playerInfo.level >= defaultv;
            case E_FunctionUnLockType.VipLevel: return player.vip.level >= defaultv;
            case E_FunctionUnLockType.StageIndex: return player.battleStage.battleStageMaxId >= defaultv;
            // case E_FunctionUnLockType.Authority: return player.vip.level >= v;
            case E_FunctionUnLockType.PraySkillLevel: return player.getPrayLevel(defaultv) >= v[1];
            case E_FunctionUnLockType.TowerStage: return SDTrainTower.getStageIndex(player.trialTower.stageId) >= defaultv;
            // case E_FunctionUnLockType.ARActivity: return player.arActivity.isARActivity;
            case E_FunctionUnLockType.InterfaceCall: return player.outerRewards.getUnlockHxxIdMap(defaultv) == 1;

            default:
                console.warn('未处理的解锁类型：' + k);
                break;
        }

        return false;
    }



    /**
     * 获取解锁提示
     * @param player 
     * @param fuctionId 
     */
    public static getFunctionUnlockString(player: Player, fuctionId: number): string {
        const config = CDFunctionUnlock.getData(fuctionId);
        if (!config) {
            return '';
        }
        let unlockStr = '';

        //开服天数
        if (config.limitDay > 0) {
            const pass = ITimer.getServerTime() - SDServer.getServerCreateTime(player);
            if (pass < config.limitDay * E_TimeConfig.Day) {
                unlockStr += '开服' + E_TimeConfig.Day + '天后';
            }
        }

        const v1 = this.getTypeUnlockString(config.type, config.value);
        if (0 === config.conditiontype) {
            if (unlockStr.length > 0) {
                unlockStr += '且';
            }
            return unlockStr + v1 + '开启';
        }

        if (unlockStr.length > 0) {
            unlockStr += '，';
        }

        const v2 = this.getTypeUnlockString(config.type_2, config.value_2);
        if (config.conditiontype === 1) {
            return unlockStr + v1 + '且' + v2 + '开启';
        }
        else {
            return unlockStr + v1 + '或' + v2 + '开启';
        }
    }



    /**
     * 获取类型解锁描述
     * @param k 
     * @param v 
     * @returns 
     */
    protected static getTypeUnlockString(k: number, v: number[]): string {
        if (k === E_FunctionUnLockType.Lock) {
            return '暂未';
        }

        const defaultv = v[0];

        switch (k) {
            case E_FunctionUnLockType.PlayerLevel: return '角色等级到达' + defaultv + '级';
            case E_FunctionUnLockType.VipLevel: return 'VIP等级到达' + defaultv + '级';
            case E_FunctionUnLockType.StageIndex: return '主线关卡到达' + defaultv + '关';
            // case E_FunctionUnLockType.Authority: return player.vip.level >= v;
            case E_FunctionUnLockType.PraySkillLevel:
                {
                    const config = CDLordPraySkill.getData(defaultv);
                    return '祝福技能' + (config ? LanguageUtils.getV(config.name) : defaultv) + '达到' + v[1] + '级';
                }
            case E_FunctionUnLockType.TowerStage: return '试炼塔到达第' + defaultv + '层';

            default:
                console.warn('未处理的解锁类型：' + k);
                break;
        }

        return '';
    }
}
